var oTable;
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
{
    if ( typeof sNewSource != 'undefined' && sNewSource != null ) {
        oSettings.sAjaxSource = sNewSource;
    }
    
    // Server-side processing should just call fnDraw
    if ( oSettings.oFeatures.bServerSide ) {
        this.fnDraw();
        return;
    }
    
    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    var iStart = oSettings._iDisplayStart;
    var aData = [];
    
    this.oApi._fnServerParams( oSettings, aData );
    
    oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
        /* Clear the old information from the table */
        that.oApi._fnClearTable( oSettings );
        
        /* Got the data - add it to the table */
        var aData =  (oSettings.sAjaxDataProp !== "") ?
        that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;
        
        for ( var i=0 ; i<aData.length ; i++ )
        {
            that.oApi._fnAddData( oSettings, aData[i] );
        }
        
        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        
        if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
        {
            oSettings._iDisplayStart = iStart;
            that.fnDraw( false );
        }
        else
        {
            that.fnDraw();
        }
        
        that.oApi._fnProcessingDisplay( oSettings, false );
        
        /* Callback user function - for event handlers etc */
        if ( typeof fnCallback == 'function' && fnCallback != null )
        {
            fnCallback( oSettings );
        }
    }, oSettings );
};       

$(document).ready(function() {
    $('#inactive').attr('onclick','showInactives()'); 
    menuClass('laboratorio');
    $.blockUI({
        message: "Aguarde un momento por favor"
    });      
    reloadTable();                
    $.unblockUI();        

} );

function reloadTable() {
    if (oTable) {
        oTable.fnDestroy();
    }
    oTable = $('#laboratorio_table').dataTable( {                                                
        "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
        },
        "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
        "sPaginationType": "bootstrap",
        aoColumnsDef: [],          
        bLengthChange: false,
        "aoColumns": [
        {
            "sTitle": "",
            sWidth: "7%"
        },
        {
            "sTitle": "Desvincular alumnos",
            sWidth: "10%"
        },
        {
            "sTitle": "M&aacute;quinas virtuales",
            sWidth: "10%"
        },
        {
            "sTitle": "Editar laboratorio",
            sWidth: "25%"
        },
        {
            "sTitle": "Descripci&oacute;n",
            sWidth: "35%"
        },
        {
            "sTitle": "C&oacute;digo matr&iacute;cula",
            sWidth: "13%"
        }
//        ,
//        {
//            "sTitle": "Creaci&oacute;n",
//            sWidth: "13%"
//        }
        ],
        "sAjaxSource": "/admin/laboratorio/list",    
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax({
                "url": sSource,
                "data": aoData,
                "success": fnCallback,
                "contentType": "application/json; charset=utf-8",
                "dataType": "json",
                "cache": false,
                "error": function (xhr, error, thrown) {                        
                    $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error! </strong>' + thrown + '</div>');
                }
            });    
        }                
    });                         

}

function deletelab(codigo_lab) {    
    bootbox.confirm("Desea eliminar el laboratorio?", function(result) {
        if(result) {
            $.ajax({
                url:'/admin/laboratorio/delete/id/' + codigo_lab,                
                type: 'post',
                dataType: 'json',                
                success: function(data) {
                    if(data.response === "error") {
                        $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error!</strong>' + data.message + '</div>');                            
                    }
                },
                error: function() {
                    $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Ocurrio un error inesperado!</strong></div>');
                }
            });
            reloadTable();
        }             
    });        
}

function add() {
    window.location.href = '/admin/laboratorio/edit/';
}

function reloadInactiveTable() {
    if (oTable) {
        oTable.fnDestroy();
    }
    oTable = $('#laboratorio_table').dataTable( {                                                
        "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
        },
        "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
        "sPaginationType": "bootstrap",
        aoColumnsDef: [],            
        "aoColumns": [
        {
            "sTitle": ""
        },
        {
            "sTitle": ""
        },
        {
            "sTitle": "Asignar alumnos"
        },
        {
            "sTitle": "Virtual Machines"
        },
        {
            "sTitle": "Nombre"
        },
        {
            "sTitle": "Descripcion"
        },
        {
            "sTitle": "Cantidad de m&aacute;quinas"
        },
        {
            "sTitle": "Creaci&oacute;n"
        }
        ],
        "sAjaxSource": "/admin/laboratorio/listinactive",    
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax({
                "url": sSource,
                "data": aoData,
                "success": fnCallback,
                "contentType": "application/json; charset=utf-8",
                "dataType": "json",
                "cache": false,
                "error": function (xhr, error, thrown) {                        
                    $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error! </strong>' + thrown + '</div>');
                }
            });    
        }                
    });                         

}

function showInactives() {
    reloadInactiveTable();
    $('#inactive').html('Laboratorios Activos...');
    $('#inactive').attr('onclick','showActives()'); 
}

function showActives() {
    reloadTable();
    $('#inactive').html('Laboratorios Inactivos...');
    $('#inactive').attr('onclick','showInactives()'); 
}

function inactivatelab(codigo_lab) {    
    bootbox.confirm("Esta accion dara inactivara el laboratorio. Desea Continuar?", function(result) {
        if(result) {
            $.ajax({
                url:'/admin/laboratorio/inactivate/id/' + codigo_lab,                
                type: 'post',
                dataType: 'json',                
                success: function(data) {
                    if(data.response == "error") {
                        $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error!</strong>' + data.message + '</div>');                            
                    }
                },
                error: function() {
                    $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Ocurrio un error inesperado!</strong></div>');
                }
            });
            reloadTable();
        }             
    });        
}